package com.example.demo.bean;

import java.io.Serializable;

/**
 * @author Smile to life
 */
public class Elvis implements Serializable {

    public static final Elvis INSTANCE = new Elvis();
    private static int numOfObJects = 0;

    {
        numOfObJects++;
    }

    private Elvis() {
        //防止反射
        if (INSTANCE != null) {
            throw new RuntimeException("不可通过反射创建对象");
        }
    }

    public static int getNumOfObJects() {
        return numOfObJects;
    }

    //防止反序列化，反序列化时候直接返回 instance,不再次创建
    private Object readResolve() {
        return INSTANCE;
    }
}
